package com.eugenes.design.cardvalidator;

import com.eugenes.collections.MyArrayList;
import com.eugenes.collections.MyList;
import com.eugenes.design.Card;

public class CompositeCardValidator implements CardValidator {

    MyList<CardValidator> validators;

    public CompositeCardValidator() {
        validators = new MyArrayList<>();
    }

    public void addValidator(CardValidator validator) {
        if (validator == null) {
            throw new IllegalArgumentException("Validator is null.");
        }
        validators.add(validator);
    }

    protected MyList<CardValidator> getValidators() {
        return validators;
    }

    @Override
    public boolean validate(Card card) {
        boolean result = true;

        for (CardValidator validator : getValidators()) {
            result &= validator.validate(card);
        }

        return result;
    }

    @Override
    public void cardIsUsed(Card card) {
        for (CardValidator validator : getValidators()) {
            validator.cardIsUsed(card);
        }
    }
}
